package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.castle.fortress.admin.cms.entity.CmsModelEntity;
import com.castle.fortress.admin.cms.mapper.CmsModelDataMapper;
import com.castle.fortress.admin.cms.service.CmsModelDataService;
import com.castle.fortress.admin.cms.service.CmsModelService;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表数据管理 服务实现类
 *
 * @author castle
 * @since 2021-11-10
 */
@Service
public class CmsModelDataServiceImpl implements CmsModelDataService {
    @Autowired
    private CmsModelDataMapper cmsModelDataMapper;
    @Autowired
    private CmsModelService cmsModelService;

    @Override
    public boolean saveData(String modelId, Map<String,Object> dataMap) {
        boolean result=false;
        if(modelId == null || dataMap==null||dataMap.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        CmsModelEntity modelEntity =cmsModelService.getById(modelId);
        String tbName = "cms_model_"+modelEntity.getCode();
        List<String> colName=new ArrayList<>();
        List<Object> colValue=new ArrayList<>();
        List<Map<String,Object>> updateList=new ArrayList<>();
        String id=dataMap.get("id").toString();
        for(String key:dataMap.keySet()){
            if(dataMap.get(key)!=null&& StrUtil.isNotEmpty(dataMap.get(key).toString())){
                Map<String,Object> m=new HashMap<>();
                if(dataMap.get(key) instanceof List) {
                    List l=(List) dataMap.get(key);
                    if(l.get(0) instanceof  Map){
                        colValue.add(new JSONArray((List) dataMap.get(key)).toString());
                        m.put("value", new JSONArray((List) dataMap.get(key)).toString());
                    }else{
                        colValue.add(CollectionUtil.join((List) dataMap.get(key), ","));
                        m.put("value", CollectionUtil.join((List) dataMap.get(key), ","));
                    }
                }else if(dataMap.get(key) instanceof Map){
                    colValue.add(new JSONObject(dataMap.get(key)).toString());
                    m.put("value", new JSONObject(dataMap.get(key)).toString());
                }else{
                    colValue.add(dataMap.get(key));
                    m.put("value",dataMap.get(key));
                }
                colName.add(key);
                m.put("name",key);
                updateList.add(m);
            }
        }
        if(!colName.isEmpty()){
            Map<String, Object> data = queryById(modelEntity.getCode(),id);
            if(data!=null){
                cmsModelDataMapper.updateData(tbName,id,updateList);
                result=true;
            }else{
                cmsModelDataMapper.saveData(tbName,colName,colValue);
                result=true;
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> queryById(String code, String id) {
        Map<String, Object> data = cmsModelDataMapper.queryById("cms_model_"+code,id);

        return data;
    }
}

